package dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;

import database.HibernateConnectionFactory;

public class BaseDAO {
	Class<?> classe;
	Session s;

	public BaseDAO(Class<?> classe) {
		this.classe = classe;
	}

	public Criteria createCriteria() {
		if (s != null) {
			closeSession();
		}
		s = HibernateConnectionFactory.openSession();
		return s.createCriteria(classe);
	}

	public Criteria createCriteria(Class<?> classe) {
		if (s != null) {
			closeSession();
		}
		s = HibernateConnectionFactory.openSession();
		return s.createCriteria(classe);
	}

	public List findAll() {
		if (s != null) {
			closeSession();
		}
		s = HibernateConnectionFactory.openSession();
		Query query = s.createQuery("from " + classe.getName());
		List list = query.list();
		return list;
	}

	protected void closeSession() {
		s.close();
	}

}
